package com.ojoin.ojoin.eb_spider.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.utils.URLUtils;
import com.ojoin.trade.common.utils.json.JSONUtils;
import com.ojoin.trade.common.web.domain.Dict;
import com.ojoin.trade.common_core.web.domain.TbMerchantInfo;
import com.ojoin.trade.supplyloan_core.web.domain.JedisObject;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanInfoVo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanRefundVo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformRule;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlStatetaskRunresultvo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;

public class WebServiceUtils {
	private static Logger log = LoggerFactory.getLogger(WebServiceUtils.class);

	public final static String host = "https://www.18join.com";
	// final String host = "http://localhost:8080";
	// platformUserId,createDateStart,createDateEnd,rowCreateTimeStart,rowCreateTimeEnd,settlementState

	// id
	public final static String queryplatformRule = host
			+ "/iplimit/queryplatformRule";

	//
	public final static String basePaymentUrl = host
			+ "/iplimit/queryPaymentMoney";

	// int joinid, String startTime,String endTime
	public final static String queryLoanRefundlist = host
			+ "/iplimit/queryLoanRefundlist";

	// platformAccountId,storeDateStart,storeDateEnd,rowCreateTimeStart,rowCreateTimeEnd,itemStatus
	public final static String baseinventoryUrl = host
			+ "/iplimit/queryinventoryMoney";

	// platformAccountId
	public final static String lastsettlementDate = host
			+ "/iplimit/getLastsettlementDate";

	// platformAccountId
	public final static String lastInsertTime = host
			+ "/iplimit/getLastInsertTime";

	// platformAccountId settlementState
	public final static String lastcreateDate = host
			+ "/iplimit/getLastcreateDate";

	// platformAccountId settlementState
	public final static String queryPaymentByUidAndCreateDate = host
			+ "/iplimit/queryPaymentByUidAndCreateDate";

	// platformAccountId settlementState
	public final static String queryInventoryByUidAndCreateDate = host
			+ "/iplimit/queryInventoryByUidAndCreateDate";

	// joinid
	public final static String queryPlatformUserByJoinid = host
			+ "/iplimit/queryPlatformUserByJoinid";

	public final static String UserUrl = host
			+ "/iplimit/queryPlatformUserById";

	// joinid
	public final static String queryMerchantUserById = host
			+ "/iplimit/queryMerchantUserById";

	public final static String queryAllTbSlLoanInfo = host
			+ "/iplimit/queryAllTbSlLoanInfo";

	// idOfTbMerchantUser=
	public final static String queryTotalNotPayback = host
			+ "/iplimit/queryTotalNotPayback";

	// label
	public final static String queryOneDict = host + "/iplimit/queryOneDict";

	// cacheName ,key
	public final static String queryJedisObject = host
			+ "/iplimit/queryJedisObject";
	// (String platformName,String merchantUserId,String statDate
	public final static String queryByPlatformAndMerchantUserIDAndDate = host
			+ "/iplimit/queryByPlatformAndMerchantUserIDAndDate";

	public static Dict queryOneDict(String label) throws Exception {
		StringBuffer url = new StringBuffer();
		url.append(queryOneDict);
		url.append("?label=" + label);

		return URLUtils.getJsonByUrl(url.toString(), Dict.class);
	}

	public static TbSlPlatformRule queryplatformRule(int id) throws Exception {
		StringBuffer url = new StringBuffer();
		url.append(queryplatformRule);
		url.append("?id=" + id);

		return URLUtils.getJsonByUrl(url.toString(), TbSlPlatformRule.class);
	}

	public static JedisObject queryJedisObject(String cacheName, String key)
			throws Exception {

		StringBuffer url = new StringBuffer();
		url.append(queryJedisObject);

		url.append("?cacheName=" + cacheName);
		url.append("&key=" + key);

		return URLUtils.getJsonByUrl(url.toString(), JedisObject.class);
	}

	public static List<TbSlLoanInfoVo> getLoanRefundList() throws Exception {
		String url = queryAllTbSlLoanInfo;

		List<TbSlLoanInfoVo> list = new ArrayList<TbSlLoanInfoVo>();

		List pays = URLUtils.getJsonByUrl(url, List.class);

		if (!CommonUtils.isEmpty(pays)) {
			Iterator it = pays.iterator();
			while (it.hasNext()) {
				Map ss = (Map) it.next();

				TbSlLoanInfoVo tbsl = JSONUtils.parseObject(
						JSONUtils.toJSONString(ss), TbSlLoanInfoVo.class);
				list.add(tbsl);

			}
		}

		return list;
	}

	public static TbMerchantInfo getTbMerchantUserByJoinId(int joinId)
			throws Exception {
		String caUrl = queryMerchantUserById + "?joinId=" + joinId;

		TbMerchantInfo ca = URLUtils.getJsonByUrl(caUrl, TbMerchantInfo.class);

		return ca;
	}

	public static String getPaymentLastcreateDate(int platformAccountId,
			String settlementState) throws Exception {
		String lasttime = URLUtils.getJsonByUrl(WebServiceUtils.lastcreateDate
				+ "?platformAccountId=" + platformAccountId
				+ "&settlementState=" + settlementState);

		return lasttime;
	}

	public static String getPaymentLastsettlementDate(int platformAccountId)
			throws Exception {
		String lasttime = URLUtils
				.getJsonByUrl(WebServiceUtils.lastsettlementDate
						+ "?platformAccountId=" + platformAccountId);

		return lasttime;
	}

	public static String getPaymentLastInsertTime(int platformAccountId)
			throws Exception {
		String lasttime = URLUtils.getJsonByUrl(WebServiceUtils.lastInsertTime
				+ "?platformAccountId=" + platformAccountId);

		return lasttime;
	}

	public static TbCatchdataStrategy getCatchdataStrategyByUserId(
			int platformUserId) throws Exception {
		String caUrl = host
				+ "/iplimit/queryCatchdataStrategyByPlatformUserId?platformAccountId="
				+ platformUserId;
		TbCatchdataStrategy ca = URLUtils.getJsonByUrl(caUrl,
				TbCatchdataStrategy.class);

		return ca;
	}

	public static long getLoanRefundMoney(int joinid, String startTime,
			String endTime) throws Exception {

		List<TbSlLoanRefundVo> list = getLoanRefundList(joinid, startTime,
				endTime);

		long money = 0;
		if (!CommonUtils.isEmpty(list)) {
			Iterator<TbSlLoanRefundVo> it = list.iterator();
			while (it.hasNext()) {
				TbSlLoanRefundVo sr = it.next();
				money += Long.valueOf(sr.getRefundAmount());
			}
		}
		return money;
	}

	public static List<TbSlLoanRefundVo> getLoanRefundList(int joinid,
			String startTime, String endTime) throws Exception {
		String url = queryLoanRefundlist + "?joinid=" + joinid + "&startTime="
				+ startTime + "&endTime=" + endTime;

		List<TbSlLoanRefundVo> list = new ArrayList<TbSlLoanRefundVo>();

		List pays = URLUtils.getJsonByUrl(url, List.class);

		if (!CommonUtils.isEmpty(pays)) {
			Iterator it = pays.iterator();
			while (it.hasNext()) {
				Map ss = (Map) it.next();

				TbSlLoanRefundVo tbsl = JSONUtils.parseObject(
						JSONUtils.toJSONString(ss), TbSlLoanRefundVo.class);
				LogUtils.info(log, tbsl.getRefundDate());
				list.add(tbsl);

			}
		}

		return list;
	}

	public static long getSupplyPaymentMoneyByplateformId(int uid,
			TbCatchdataStrategy ca, String stime, String etime, String statues)
			throws Exception {
		StringBuffer url = new StringBuffer();
		url.append(WebServiceUtils.basePaymentUrl);
		url.append("?platformAccountId=" + uid);
		url.append("&settlementState=" + statues);

		if (ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchAll)) {
			String time = TimeUtils.getCurDate();
			url.append("&rowCreateTimeStart=" + time);
			url.append("&rowCreateTimeEnd=" + time);

		} else if (ca.getPaymentCatchAllOrPart().equals(
				TbCatchdataStrategy.CatchAllPart)) {
			String time = TimeUtils.getCurDate();
			url.append("&rowCreateTimeStart=" + time);
			url.append("&rowCreateTimeEnd=" + time);
			if (!CommonUtils.isEmpty(stime) && !CommonUtils.isEmpty(etime)) {
				url.append("&createDateStart=" + stime);
				url.append("&createDateEnd=" + etime);
			}

		} else {
			if (!CommonUtils.isEmpty(stime) && !CommonUtils.isEmpty(etime)) {
				url.append("&createDateStart=" + stime);
				url.append("&createDateEnd=" + etime);
			}
		}

		String paymoney = URLUtils.getJsonByUrl(url.toString());

		return Long.valueOf(paymoney);
	}

	public static long getSupplyInventoryMoneyByplateformId(int uid,
			TbCatchdataStrategy ca, String stime, String etime,
			String itemStatus) throws Exception {

		StringBuffer url = new StringBuffer();

		url.append(WebServiceUtils.baseinventoryUrl);
		url.append("?platformAccountId=" + uid);
		url.append("&itemStatus=" + itemStatus);

		if (ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchAll)) {
			String time = TimeUtils.getCurDate();
			url.append("&rowCreateTimeStart=" + time);
			url.append("&rowCreateTimeEnd=" + time);

		} else if (ca.getPaymentCatchAllOrPart().equals(
				TbCatchdataStrategy.CatchAllPart)) {
			String time = TimeUtils.getCurDate();
			url.append("&rowCreateTimeStart=" + time);
			url.append("&rowCreateTimeEnd=" + time);
			if (!CommonUtils.isEmpty(stime) && !CommonUtils.isEmpty(etime)) {
				url.append("&storeDateStart=" + stime);
				url.append("&storeDateEnd=" + etime);
			}

		} else {
			if (!CommonUtils.isEmpty(stime) && !CommonUtils.isEmpty(etime)) {
				url.append("&storeDateStart=" + stime);
				url.append("&storeDateEnd=" + etime);
			}
		}
		String paymoney = URLUtils.getJsonByUrl(url.toString());

		return Long.valueOf(paymoney);
	}

	public static long getLiabilitiesByJoinId(int joinId) throws Exception {
		String url = "https://www.18join.com/iplimit/queryTotalNotPayback?idOfTbMerchantUser="
				+ joinId;

		String money = URLUtils.getJsonByUrl(url);

		return Long.valueOf(money);
	}

	public static List<TbSlSupplyPayment> getSupplyPaymentByplateformId(
			int uid, TbCatchdataStrategy ca, String stime, String etime,
			String statues) throws Exception {
		List<TbSlSupplyPayment> list = new ArrayList<TbSlSupplyPayment>();
		StringBuffer url = new StringBuffer();

		url.append(WebServiceUtils.queryPaymentByUidAndCreateDate);
		url.append("?platformAccountId=" + uid);
		url.append("&settlementState=" + statues);

		if (ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchAll)) {
			String time = TimeUtils.getCurDate();
			url.append("&rowCreateTimeStart=" + time);
			url.append("&rowCreateTimeEnd=" + time);
		} else {
			if (!CommonUtils.isEmpty(stime) && CommonUtils.isEmpty(etime)) {
				url.append("&createDateStart=" + stime);
				url.append("&createDateEnd=" + etime);
			}

		}

		List pays = URLUtils.getJsonByUrl(url.toString(), List.class);

		if (!CommonUtils.isEmpty(pays)) {
			Iterator it = pays.iterator();
			while (it.hasNext()) {
				Map ss = (Map) it.next();
				TbSlSupplyPayment tbsl = JSONUtils.parseObject(
						JSONUtils.toJSONString(ss), TbSlSupplyPayment.class);
				list.add(tbsl);

			}

		}

		return list;
	}

	// int platformAccountId,
	// String rowCreateTimeStart, String rowCreateTimeEnd,
	// String storeDateStart, String storeDateEnd, String itemStatus
	public static Map<String, TbSlSupplyInventory> getSupplyInventoryByplateformId(
			int uid, String stime, String etime, String statues)
			throws Exception {
		Map<String, TbSlSupplyInventory> map = new HashMap<String, TbSlSupplyInventory>();
		TbCatchdataStrategy ca = getCatchdataStrategyByUserId(uid);

		StringBuffer url = new StringBuffer();
		url.append(WebServiceUtils.queryInventoryByUidAndCreateDate);

		if (CommonUtils.isEmpty(uid)) {
			throw new BusinessException("uid can not be null");
		} else {
			url.append("?platformAccountId=" + uid);
		}

		if (ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchAll)) {
			String time = TimeUtils.getCurDate();
			url.append("&rowCreateTimeStart=" + time);
			url.append("&rowCreateTimeEnd=" + time);

		}
		if (!CommonUtils.isEmpty(stime)) {
			url.append("&storeDateStart=" + stime);
		}

		if (!CommonUtils.isEmpty(etime)) {
			url.append("&storeDateEnd=" + etime);
		}

		if (!CommonUtils.isEmpty(statues)) {
			url.append("&itemStatus=" + statues);
		}

		List pays = URLUtils.getJsonByUrl(url.toString(), List.class);

		if (!CommonUtils.isEmpty(pays)) {
			Iterator it = pays.iterator();
			while (it.hasNext()) {
				Map ss = (Map) it.next();
				TbSlSupplyInventory tbsl = JSONUtils.parseObject(
						JSONUtils.toJSONString(ss), TbSlSupplyInventory.class);
				map.put(tbsl.getItemKey(), tbsl);

			}

		}

		return map;
	}

	public static List<TbSlPlatformUser> getPlatformUserList(int joinid)
			throws Exception {
		List<TbSlPlatformUser> list = new ArrayList<TbSlPlatformUser>();

		List users = URLUtils
				.getJsonByUrl(WebServiceUtils.queryPlatformUserByJoinid
						+ "?joinid=" + joinid, List.class);
		if (!CommonUtils.isEmpty(users)) {
			Iterator it = users.iterator();
			while (it.hasNext()) {
				Map ss = (Map) it.next();
				TbSlPlatformUser tbsl = JSONUtils.parseObject(
						JSONUtils.toJSONString(ss), TbSlPlatformUser.class);
				list.add(tbsl);
			}

		}

		return list;

	}

	public static TbSlPlatformUser getPlatformUser(int platformUserId)
			throws Exception {
		TbSlPlatformUser user = URLUtils.getJsonByUrl(UserUrl
				+ "?platformAccountId=" + platformUserId,
				TbSlPlatformUser.class);
		return user;

	}

	public static Map<Integer, List> getSupplyPaymentBymerchatId(int merchatId,
			String stime, String etime, String statues) throws Exception {

		Map<Integer, List> all = new HashMap<Integer, List>();
		List<TbSlPlatformUser> users = getPlatformUserList(merchatId);

		if (!CommonUtils.isEmpty(users)) {
			Iterator<TbSlPlatformUser> it = users.iterator();
			while (it.hasNext()) {
				TbSlPlatformUser tbsl = it.next();
				TbCatchdataStrategy ca = getCatchdataStrategyByUserId(tbsl
						.getId());
				List li = getSupplyPaymentByplateformId(tbsl.getId(), ca,
						stime, etime, statues);

				all.put(tbsl.getId(), li);
			}

		}
		return all;
	}

	public static Long getTotalNotPayback(int idOfTbMerchantUser)
			throws Exception {
		return URLUtils.getJsonByUrl(queryTotalNotPayback
				+ "?idOfTbMerchantUser=" + idOfTbMerchantUser, Long.class);

	}

	
	public static Map<Integer,TbSlStatetaskRunresultvo> queryByPlatformAndMerchantUserIDAndDate(
			String platformName, String merchantUserId, String statDate)
			throws Exception {
		
		Map<Integer,TbSlStatetaskRunresultvo> map = null;
		StringBuffer url = new StringBuffer();
		url.append(WebServiceUtils.queryByPlatformAndMerchantUserIDAndDate);
		url.append("?1=1");
		if(!CommonUtils.isEmpty(platformName)){
			url.append("&platformName=" + platformName);
		}
		
		url.append("&merchantUserId=" + merchantUserId);
		url.append("&statDate=" + statDate);

		List li = URLUtils.getJsonByUrl(url.toString(), List.class);

		if (!CommonUtils.isEmpty(li)) {
			map = new HashMap<Integer,TbSlStatetaskRunresultvo>();
			Iterator it = li.iterator();
			while (it.hasNext()) {
				Map ss = (Map) it.next();
				TbSlStatetaskRunresultvo tbsl = JSONUtils.parseObject(
						JSONUtils.toJSONString(ss),
						TbSlStatetaskRunresultvo.class);
				map.put(tbsl.getIdOfPlatformUser(), tbsl);
			}

		}
		
		return map;
	}
	
	

}
